import Vue from "vue";
import Router from "vue-router";
import Layout from "@/views/home"
import problemOrder from '@/router/problemOrder';
import org from '@/router/org';
import eCharts from '@/router/eCharts';

Vue.use(Router);

const router = new Router({
    //   mode: "history", // 去掉url中的#
    routes: [{
            path: "/",
            name: "index",
            component: Layout,
            // 嵌套路由
            children: [{
                path: "home",
                name: "home",
                meta: { title: "首页" },
                component: () =>
                    import ("@/views/formTest")
            }]
        },
        // 登录
        {
            path: "/login",
            name: "login",
            component: () =>
                import ("@/views/login")
        },
        {
            path: "todo",
            name: "todo",
            component: () =>
                import ("@/views/toDo/index.vue")
        },
        ...problemOrder,
        ...org,
        ...eCharts
    ]
});
const whiltPathName = ["login", "Home", "friend"];
/**
 * 路由守卫
 */
router.beforeEach((to, from, next) => {
    let token = localStorage.getItem("token");
    if (!token) {
        console.log("token存在");
        next();
    } else {
        next();
        // 解决路由无限重定向
        if(to.path === '/login') next()
        //路由白名单放行
        if (whiltPathName.indexOf(to.name) != -1) {
            console.log("是白名单，放行");
            next();
        } else {
            console.log("token不存在，不是白名单，请登录！");
            //跳转到登录页面
            next('/login')
        }
    }
});
export default router;